package com.neuedu.outpatient01.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.neuedu.outpatient01.entity.CheckApply;
import com.neuedu.outpatient01.entity.Fmeditem;
import com.neuedu.outpatient01.entity.Medicalrecord;
import com.neuedu.outpatient01.entity.Patientcosts;
import com.neuedu.outpatient01.entity.Register;
import com.neuedu.outpatient01.mapper.FmeditemMapper;
import com.neuedu.outpatient01.service.CheckApplyService;
import com.neuedu.outpatient01.service.FmeditemService;
import com.neuedu.outpatient01.service.MedicalrecordService;
import com.neuedu.outpatient01.service.RegisterService;

@RestController
@RequestMapping("/inspectionApplication")
public class InspectionApplication {
	
	@Autowired
	private RegisterService rservice;
	
	@Autowired 
	private FmeditemService fservice;
	
	@Autowired
	private CheckApplyService cservice;
	
	@Autowired
	private MedicalrecordService mservice;
	//查找未却确诊患者
	@RequestMapping("/selectNoDiagnosisRegister")
	//http://localhost:8090/inspectionApplication/selectNoDiagnosisRegister
	public List<Register> selectNoDiagnosisRegister(Register r){
		return rservice.selectNoDiagnosisRegister(r);
	}
	//查找非药品项目的列表
	@RequestMapping("/inquireInspection")
	//http://localhost:8090/inspectionApplication/inquireInspection
	public List<Fmeditem> inquireInspection(Fmeditem f){
		return fservice.inquireInspection(f);
	}
	
	//插入信息到CheckApply表
	public int insertCheckApply(CheckApply c) {
		return cservice.insertCheckApply(c);
	}
	//根据病历ID更新CheckApply表
	public int updateCheckApply(CheckApply c){
		return cservice.updateCheckApply(c);
	}
	//删除CheckApply表中的某行内容
	public int deleteCheckApply(CheckApply c) {
		return cservice.deleteCheckApply(c);
	}
	//根据挂号ID查找Register表的信息
	public Register inquireRegister(int id) {
		return rservice.inquireRegister(id);
	}
	//根据病历号查找Medicalrecord表的信息
	public Medicalrecord inquireHistory(String CaseNumber) {
		return mservice.inquireHistory(CaseNumber);
	}
	//查询CheckApply表中是否有信息
	public CheckApply inquireCheckApply(CheckApply c) {
		return cservice.inquireCheckApply(c);
	}
	//查询项目费用
	public Fmeditem inquireSingleInspection(Integer ID) {
		return fservice.inquireSingleInspection(ID);
	}
	//增加进项目费用明细表
	public int insertPatientcosts(Patientcosts p) {
		return cservice.insertPatientcosts(p);
	}
	//暂存项目
	@RequestMapping("/TemporaryItems")
	//http://localhost:8090/inspectionApplication/TemporaryItems?ID=64&RegistID=3&ItemID=10&Name=洗胃&CheckOperID=2&ResultOperID=2&State=1
	public String TemporaryItems(CheckApply c) {
		System.out.println("--c="+c);
		if(inquireRegister(c.getRegistID())!=null) {
			System.out.println("----------------------------------------");
			Register r = inquireRegister(c.getRegistID());
			System.out.println("52252532555253"+r.getCaseNumber());
			if(inquireHistory(r.getCaseNumber())!=null) {
				Medicalrecord m = inquireHistory(r.getCaseNumber());
				CheckApply ca = new CheckApply();
				ca = c;
				ca.setMedicalID(m.getID());
				ca.setDoctorID(r.getUserID());
				Date day=new Date (); 
				SimpleDateFormat df = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss"); 
				System.out.println("-----------------------");
				System.out.println (df.format (day));
				System.out.println(day);
				ca.setCreationTime(df.format(day));
				ca.setRecordType(1);
				ca.setCheckOperID(2);
				ca.setResultOperID(2);
				Patientcosts p = new Patientcosts();
				p.setRegistID(ca.getRegistID());
				p.setInvoiceID(0);
				p.setItemID(ca.getID());
				p.setItemType(1);
				p.setName(ca.getName());
				p.setPrice(inquireSingleInspection(ca.getItemID()).getPrice());
				p.setAmount(ca.getNum());
				p.setDeptID(1);
				p.setCreatetime(ca.getCreationTime());
				p.setCreateOperID(301);
				if(inquireCheckApply(ca)==null) {
					if(ca.getState()==2) {
						insertPatientcosts(p);
					}
					insertCheckApply(ca);
					return "插入成功";
				}else {
					if(inquireCheckApply(ca).getState()==2) {
						return "你已经将项目立项了";
					}
					if(ca.getState()==2) {
						insertPatientcosts(p);
					}
					updateCheckApply(ca);
					return "更新成功";
				}
			}else {
				return "失败";
			}
		}else {
			return "失败";
		}
		
	}
	//修改项目
	@RequestMapping("/UpdateInspectionApplication")
	//http://localhost:8090/inspectionApplication/UpdateInspectionApplication?ID=70&RegistID=5&ItemID=12&Name=灌肠&CheckOperID=2&ResultOperID=2&State=1
	public String UpdateInspectionApplication(CheckApply c) {
		System.out.println("11111111111111111111111111111111111111111111111");
		System.out.println("--c="+c);
		Register r = inquireRegister(c.getRegistID());
		System.out.println("--c="+c);
		if(c.getState()>=2) {
			return "项目已经开立，不能修改";
		}else {
			System.out.println("-------------------------------------");
			CheckApply ca = new CheckApply();
			Medicalrecord m = inquireHistory(r.getCaseNumber());
			ca = c;
			Date day=new Date (); 
			SimpleDateFormat df = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss"); 
			System.out.println (df.format (day));
			System.out.println(day);
			ca.setCreationTime(df.format(day));
			ca.setRecordType(1);
			ca.setDoctorID(r.getUserID());
			ca.setMedicalID(m.getID());
			System.out.println("ca:"+ca);
			updateCheckApply(ca);
			return "修改成功";
		}
	}
	//删除项目
	@RequestMapping("/DeleteInspectionApplication")
	//http://localhost:8090/inspectionApplication/DeleteInspectionApplication
	public String DeleteInspectionApplication(CheckApply c) {
		if(c.getState()<2) {
			deleteCheckApply(c);
		    return "ok";
		}else {
			return "项目已开立，不能删除";
		}
	}
	//作废项目
	@RequestMapping("/NullifyInspectionApplication")
    //http://localhost:8090/inspectionApplication/NullifyInspectionApplication
	public String NullifyInspectionApplication(CheckApply c) {
		if(c.getState()<4) {
			deleteCheckApply(c);
		    return "ok";
		}else {
			return "项目已登记，不能作废";
		}
	}
	//查看项目结果
	@RequestMapping("/InquireInspectionApplication")
	//http://localhost:8090/inspectionApplication/InquireInspectionApplication
	public CheckApply InquireInspectionApplication(CheckApply c) {
		System.out.println("--c="+c);
		return inquireCheckApply(c);
	}
	//查看已选择
	@RequestMapping("/InquireCheckApply")
	//http://localhost:8090/inspectionApplication/InquireCheckApply
	public List<CheckApply> InquireCheckApply(Register r){
		return cservice.InquireCheckApply(r);
	}
}
